<template>
  <div class="inventoryPosting">
    <div class="header">
      <div class="header-name">
        <el-form :model="proVal" label-width="100px" size="mini">
          <el-form-item :label="$t('market.stareData')">
            <el-date-picker
              v-model="proVal.startDate"
              type="date"
              value-format="yyyy-MM-dd"
              style="width: 175px"
            />
          </el-form-item>
          <el-form-item :label="$t('market.endData')">
            <el-date-picker
              v-model="proVal.endDate"
              type="date"
              value-format="yyyy-MM-dd"
              style="width: 175px"
            />
          </el-form-item>
          <el-form-item :label="$t('market.order')">
            <el-input v-model.trim="proVal.fbillno" />
          </el-form-item>
          <el-form-item :label="$t('market.paezText1')">
            <el-input v-model.trim="proVal.khddk" />
          </el-form-item>
          <el-form-item label-width="50px">
            <el-button type="primary" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
            <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
          </el-form-item>
        </el-form>
        <!--        <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />-->
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        get-row-keys="FENTRYID"
        serial
        sortable
        :get-summaries="getSummaries"
        show-summary
        :cell-style="cellStyle"
        :header-cell-style="cellStyle1"
      >
        <el-table-column :label="$t('market.date')" prop="销售订单日期" align="center" width="120" />
        <el-table-column :label="$t('market.order')" prop="销售订单号" align="center" width="120" />
        <el-table-column :label="$t('market.paezText1')" prop="客户订单号" align="center" width="110" />
        <el-table-column :label="$t('market.saler')" prop="销售员" align="center" width="100" />
        <el-table-column :label="$t('market.frowType')" prop="产品类型" align="center" width="100" />
        <el-table-column :label="$t('market.fmodel2')" prop="助记码" align="center" width="120" />
        <el-table-column :label="$t('market.fmaterial')" prop="物料编码" align="center" width="120">
          <template slot-scope="scope">
            <el-link type="primary" @click="handleInto(scope.row.物料编码)">
              {{ scope.row.物料编码 }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column :label="$t('shop.fname2')" prop="物料名称" align="center" width="120" />
        <el-table-column :label="$t('shop.fspecificaTion')" prop="物料规格" align="center" width="120" />
        <el-table-column :label="$t('shop.fdescripTion')" prop="物料描述" align="center" width="300" />
        <el-table-column :label="$t('market.fqty2')" prop="销售数量" align="center" width="100" />
        <el-table-column :label="$t('market.cprks')" prop="成品入库数" align="center" width="100" />
        <el-table-column :label="$t('market.salesDelivery')" prop="销售交期" align="center" width="100" />
        <el-table-column :label="$t('market.fplanfinishdate')" prop="生产计划交期" align="center" width="100" />
        <el-table-column label="脚架粗胚" align="center">
          <el-table-column :label="$t('market.shouldNum')" prop="应领数量1" align="center" width="80">
            <template slot-scope="scope">
              <el-link v-if="scope.row.应领数量1===scope.row.已领数量1" type="primary">{{ scope.row.应领数量1 }}</el-link>
              <el-link v-else type="danger">{{ scope.row.应领数量1 }}</el-link>
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.alreadyQty')" prop="已领数量1" align="center" width="80" />
          <el-table-column :label="$t('market.stockQty')" prop="库存数量1" align="center" width="80" />
          <el-table-column :label="$t('market.stockUnit')" prop="库存单位1" align="center" width="80" />
          <el-table-column :label="$t('market.scrks')" prop="生产入库数1" align="center" width="80" />
        </el-table-column>
        <el-table-column label="脚架烤漆" align="center">
          <!--          <el-table-column :label="$t('market.zxwwllb')" prop="子项物料类别2" align="center" width="80" />-->
          <el-table-column :label="$t('market.shouldNum')" prop="应领数量2" align="center" width="80">
            <template slot-scope="scope">
              <el-link v-if="scope.row.应领数量2===scope.row.已领数量2" type="primary">{{ scope.row.应领数量2 }}</el-link>
              <el-link v-else type="danger">{{ scope.row.应领数量2 }}</el-link>
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.alreadyQty')" prop="已领数量2" align="center" width="80" />
          <el-table-column :label="$t('market.stockQty')" prop="库存数量2" align="center" width="80" />
          <el-table-column :label="$t('market.stockUnit')" prop="库存单位2" align="center" width="80" />
          <el-table-column :label="$t('market.scrks')" prop="生产入库数2" align="center" width="80" />
        </el-table-column>
        <el-table-column label="台面" align="center">
          <!--          <el-table-column :label="$t('market.zxwwllb')" prop="子项物料类别3" align="center" width="80" />-->
          <el-table-column :label="$t('market.shouldNum')" prop="应领数量3" align="center" width="80">
            <template slot-scope="scope">
              <el-link v-if="scope.row.应领数量3===scope.row.已领数量3" type="primary">{{ scope.row.应领数量3 }}</el-link>
              <el-link v-else type="danger">{{ scope.row.应领数量3 }}</el-link>
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.alreadyQty')" prop="已领数量1" align="center" width="80" />
          <el-table-column :label="$t('market.stockQty')" prop="库存数量1" align="center" width="80" />
          <el-table-column :label="$t('market.stockUnit')" prop="库存单位1" align="center" width="80" />
          <el-table-column :label="$t('market.scrks')" prop="生产入库数1" align="center" width="80" />
        </el-table-column>
        <el-table-column label="框架粗胚" align="center">
          <!--          <el-table-column :label="$t('market.zxwwllb')" prop="子项物料类别4" align="center" width="80" />-->
          <el-table-column :label="$t('market.shouldNum')" prop="应领数量4" align="center" width="80">
            <template slot-scope="scope">
              <el-link v-if="scope.row.应领数量4===scope.row.已领数量4" type="primary">{{ scope.row.应领数量4 }}</el-link>
              <el-link v-else type="danger">{{ scope.row.应领数量4 }}</el-link>
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.alreadyQty')" prop="已领数量4" align="center" width="80" />
          <el-table-column :label="$t('market.stockQty')" prop="库存数量4" align="center" width="80" />
          <el-table-column :label="$t('market.stockUnit')" prop="库存单位4" align="center" width="80" />
          <el-table-column :label="$t('market.scrks')" prop="生产入库数4" align="center" width="80" />
        </el-table-column>
        <el-table-column label="框架烤漆" align="center">
          <!--          <el-table-column :label="$t('market.zxwwllb')" prop="子项物料类别5" align="center" width="80" />-->
          <el-table-column :label="$t('market.shouldNum')" prop="应领数量5" align="center" width="80">
            <template slot-scope="scope">
              <el-link v-if="scope.row.应领数量5===scope.row.已领数量5" type="primary">{{ scope.row.应领数量5 }}</el-link>
              <el-link v-else type="danger">{{ scope.row.应领数量5 }}</el-link>
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.alreadyQty')" prop="已领数量5" align="center" width="80" />
          <el-table-column :label="$t('market.stockQty')" prop="库存数量5" align="center" width="80" />
          <el-table-column :label="$t('market.stockUnit')" prop="库存单位5" align="center" width="80" />
          <el-table-column :label="$t('market.scrks')" prop="生产入库数5" align="center" width="80" />
        </el-table-column>
        <el-table-column label="中柱粗胚" align="center">
          <!--          <el-table-column :label="$t('market.zxwwllb')" prop="子项物料类别6" align="center" width="80" />-->
          <el-table-column :label="$t('market.shouldNum')" prop="应领数量6" align="center" width="80">
            <template slot-scope="scope">
              <el-link v-if="scope.row.应领数量6===scope.row.已领数量6" type="primary">{{ scope.row.应领数量6 }}</el-link>
              <el-link v-else type="danger">{{ scope.row.应领数量6 }}</el-link>
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.alreadyQty')" prop="已领数量6" align="center" width="80" />
          <el-table-column :label="$t('market.stockQty')" prop="库存数量6" align="center" width="80" />
          <el-table-column :label="$t('market.stockUnit')" prop="库存单位6" align="center" width="80" />
          <el-table-column :label="$t('market.scrks')" prop="生产入库数6" align="center" width="80" />
        </el-table-column>
        <el-table-column label="中柱加工" align="center">
          <!--          <el-table-column :label="$t('market.zxwwllb')" prop="子项物料类别7" align="center" width="80" />-->
          <el-table-column :label="$t('market.shouldNum')" prop="应领数量7" align="center" width="80">
            <template slot-scope="scope">
              <el-link v-if="scope.row.应领数量7===scope.row.已领数量7" type="primary">{{ scope.row.应领数量7 }}</el-link>
              <el-link v-else type="danger">{{ scope.row.应领数量7 }}</el-link>
            </template>
          </el-table-column>
          <el-table-column :label="$t('market.alreadyQty')" prop="已领数量7" align="center" width="80" />
          <el-table-column :label="$t('market.stockQty')" prop="库存数量7" align="center" width="80" />
          <el-table-column :label="$t('market.stockUnit')" prop="库存单位7" align="center" width="80" />
          <el-table-column :label="$t('market.scrks')" prop="生产入库数7" align="center" width="80" />
        </el-table-column>
      </jc-table>
    </div>
  </div>
</template>

<script>
import {
  queryDiningTable
} from '@/api/financial/salesMargin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel2'
import searData from '@/components/Search/mixin'
import picker from '@/components/Search/picker'

export default {
  mixins: [searData, picker],
  data() {
    return {
      proVal: {
        fbillno: '',
        khddk: '',
        startDate: '',
        endDate: ''
      },
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [],
      tableHeader1: []
    }
  },
  created() {
    this.benyue() // mixins picker
    this.proVal.endDate = this.timers[1]
    this.proVal.startDate = this.timers[0]
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 200px)' : '73vh'
  },
  methods: {
    // 单元格背景 style
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (columnIndex > 14 && columnIndex < 20) { // 皮布料
        return { background: 'rgba(253,232,131,0.42)!important' }
      } else if (columnIndex > 19 && columnIndex < 25) { // 海绵
        return { background: 'rgba(153,232,131,0.42)!important' }
      } else if (columnIndex > 24 && columnIndex < 30) { // 座板
        return { background: 'rgba(253,232,131,0.42)!important' }
      } else if (columnIndex > 29 && columnIndex < 35) { // 软体
        return { background: 'rgba(153,232,131,0.42)!important' }
      } else if (columnIndex > 34 && columnIndex < 40) { // 下架
        return { background: 'rgba(253,232,131,0.42)!important' }
      } else if (columnIndex > 39 && columnIndex < 45) { // 烤漆
        return { background: 'rgba(153,232,131,0.42)!important' }
      } else if (columnIndex > 44 && columnIndex < 50) { // 裁床
        return { background: 'rgba(253,232,131,0.42)!important' }
      }
    },
    // 表头背景 style
    cellStyle1({ row, column, rowIndex, columnIndex }) {
      const bag = [15, 17, 19, 21]
      const bags = [16, 18, 20]
      if (rowIndex === 0) {
        if (bag.includes(columnIndex)) { // 皮布料
          return { background: 'rgba(253,232,131,0.42)!important' }
        } else if (bags.includes(columnIndex)) { // 皮布料
          return { background: 'rgba(153,232,131,0.42)!important' }
        } else {
          return { background: '#FFF!important' }
        }
      }
      if (rowIndex === 1) {
        if (columnIndex >= 0 && columnIndex < 5) { // 皮布料
          return { background: 'rgba(253,232,131,0.42)!important' }
        } else if (columnIndex > 4 && columnIndex < 10) { // 海绵
          return { background: 'rgba(153,232,131,0.42)!important' }
        } else if (columnIndex > 9 && columnIndex < 15) { // 座板
          return { background: 'rgba(253,232,131,0.42)!important' }
        } else if (columnIndex > 14 && columnIndex < 20) { // 软体
          return { background: 'rgba(153,232,131,0.42)!important' }
        } else if (columnIndex > 19 && columnIndex < 25) { // 打磨下架
          return { background: 'rgba(253,232,131,0.42)!important' }
        } else if (columnIndex > 24 && columnIndex < 30) { // 下架烤漆
          return { background: 'rgba(153,232,131,0.42)!important' }
        } else if (columnIndex > 29 && columnIndex < 35) { // 裁床
          return { background: 'rgba(253,232,131,0.42)!important' }
        } else {
          return { background: '#FFF!important' }
        }
      }
    },
    // 获取餐台欠料一览表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        ...this.proVal,
        pageNum: this.currentPage,
        pageSize: this.size
      }
      const { data: RES, code, message } = await queryDiningTable(DATA)
      if (code === 1) {
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        return
      }
      this.tableData = RES
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 物料详情
    handleInto(ev) {
      this.$router.push({ path: `/detailMateriel/${ev}` })
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const multiHeader = [[
          '销售订单日期', '销售订单号', '客户订单号', '销售员', '助记码', '物料编码', '物料名称', '物料规格', '物料描述', '销售数量',
          '销售交期', '脚架粗胚', '', '', '', '', '', '脚架烤漆', '', '', '', '', '', '台面', '', '', '', '', '',
          '框架粗胚', '', '', '', '', '', '框架烤漆', '', '', '', '', '', '中柱粗胚', '', '', '', '', '',
          '中柱加工', '', '', '', '', ''
        ], [
          '', '', '', '', '', '', '', '', '', '', '',
          '子项物料类别1', '应领数量1', '已领数量1', '库存数量1', '库存单位1', '生产入库数1',
          '子项物料类别2', '应领数量2', '已领数量2', '库存数量2', '库存单位2', '生产入库数2',
          '子项物料类别3', '应领数量3', '已领数量3', '库存数量3', '库存单位3', '生产入库数3',
          '子项物料类别4', '应领数量4', '已领数量4', '库存数量4', '库存单位4', '生产入库数4',
          '子项物料类别5', '应领数量5', '已领数量5', '库存数量5', '库存单位5', '生产入库数5',
          '子项物料类别6', '应领数量6', '已领数量6', '库存数量6', '库存单位6', '生产入库数6',
          '子项物料类别7', '应领数量7', '已领数量7', '库存数量7', '库存单位7', '生产入库数7'
        ]]
        const tHeader = []
        const filterVal = [
          '销售订单日期', '销售订单号', '客户订单号', '销售员', '助记码', '物料编码', '物料名称', '物料规格', '物料描述', '销售数量',
          '销售交期', '子项物料类别1', '应领数量1', '已领数量1', '库存数量1', '库存单位1', '生产入库数1',
          '子项物料类别2', '应领数量2', '已领数量2', '库存数量2', '库存单位2', '生产入库数2',
          '子项物料类别3', '应领数量3', '已领数量3', '库存数量3', '库存单位3', '生产入库数3',
          '子项物料类别4', '应领数量4', '已领数量4', '库存数量4', '库存单位4', '生产入库数4',
          '子项物料类别5', '应领数量5', '已领数量5', '库存数量5', '库存单位5', '生产入库数5',
          '子项物料类别6', '应领数量6', '已领数量6', '库存数量6', '库存单位6', '生产入库数6',
          '子项物料类别7', '应领数量7', '已领数量7', '库存数量7', '库存单位7', '生产入库数7'
        ]
        const merges = [
          'A1:A2', 'B1:B2', 'C1:C2', 'D1:D2', 'E1:E2', 'F1:F2', 'G1:G2', 'H1:H2', 'I1:I2', 'J1:J2', 'K1:K2',
          'L1:Q1', 'R1:W1', 'X1:AC1', 'AD1:AI1', 'AJ1:AO1', 'AP1:AU1', 'AV1:BA1'
        ]
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          const list = this.tableData
          const data = formatJson(filterVal, list)
          export_json_to_excel({ multiHeader, header: tHeader, data, merges, filename: '餐台欠料一览表' })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.inventoryPosting {
  //@include listBom;
  padding: 0;
  .header{
    position:relative;
    .header-name{
      width: 100%;
      span {
        width: 100px;
        font-size: 12px;
        text-align: center;
      }
    }
    .el-form {
      display: flex;
      flex-wrap: wrap;

      .el-form-item {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        width: 275px;
        margin-bottom: 5px;
      }
    }
    .btn{
      transform: translateY(18%);
      margin-left: 15px !important;
    }
  }
}
</style>
